<template>
  <div style="width: 100%;">
    <div style="margin-bottom: 30px;margin-left: 30px"><h2>编辑图书</h2></div>
    <el-form :inline="true" :model="form" :rules="rules" ref="ruleForm" label-width="100px" style="margin-left: 50px;"
             size="max">
      <el-form-item label="图书标准码" prop="bookNo">
        <el-select v-model="form.bookNo" filterable placeholder="请选择" @change="selectBook">
          <el-option
              v-for="item in books"
              :key="item.id"
              :label="item.bookNo"
              :value="item.bookNo">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="图书名称" prop="bookName">
        <el-input prefix-icon="el-icon-user" disabled v-model="form.bookName" placeholder="请输入图书名称"></el-input>
      </el-form-item>
      <el-form-item label="所需积分" prop="score">
        <el-input prefix-icon="el-icon-user" disabled v-model="form.score" placeholder="请输入图书名称"></el-input>
      </el-form-item>
      <el-form-item label="图书数量" prop="numbers">
        <el-input prefix-icon="el-icon-user" disabled v-model="form.numbers" placeholder="请输入图书数量"></el-input>
      </el-form-item>
      <el-form-item label="会员码" prop="userNo">
        <el-select v-model="form.userNo" filterable placeholder="请选择" @change="selectUser">
          <el-option
              v-for="item in users"
              :key="item.id"
              :label="item.username"
              :value="item.username">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="用户名称" prop="userName">
        <el-input prefix-icon="el-icon-phone-outline" disabled v-model="form.userName"
                  placeholder="请输入出版社"></el-input>
      </el-form-item>
      <el-form-item label="用户联系方式" prop="userPhone">
        <el-input prefix-icon="el-icon-phone-outline" disabled v-model="form.userPhone"
                  placeholder="请输入出版社"></el-input>
      </el-form-item>
      <el-form-item label="用户账户积分" prop="account">
        <el-input prefix-icon="el-icon-phone-outline" disabled v-model="form.account"></el-input>
      </el-form-item>
      <el-form-item label="借出天数" prop="days">
        <el-input-number v-model="form.days" :min="1" :max="30" label="借出天数"></el-input-number>
      </el-form-item>
    </el-form>

    <div style="text-align: center;margin-top: 20px">
      <el-button type="primary" @click="save" size="medium">提交</el-button>
    </div>
  </div>
</template>
<script>
import request from "@/utils/request";

export default {
  name: "EditBorrow",
  data() {
    return {
      form: {},
      books: [],
      users: [],
      rules: {
        bookNo: [
          {required: true, message: '图书标准码不能为空', trigger: 'blur'},
        ],
        userNo: [
          {required: true, message: '用户会员码不能为空', trigger: 'blur'},
        ],
      }
    }
  },
  created() {
    request.get('/book/list').then(res => {
      this.books = res.data
    })

    request.get('/user/list').then(res => {
      this.users = res.data.filter(v => v.status)
    })

    const id = this.$route.query.id   //获取到查询参数id
    request.get("/borrow/" + id).then(res => {
      this.form = res.data
    })
  },
  methods: {
    save() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          request.put('/borrow/update', this.form).then(res => {
            if (res.code === '200') {
              this.$notify.success('修改成功！')
              this.$router.push("/book")   //跳转到列表界面
            } else {
              this.$notify.error('修改失败！' + res.msg)
            }
          })
        }
      })
    },
    selectBook() { //获取到图书标准码
      const book = this.books.find(v => v.bookNo === this.form.bookNo)
      console.log(book)
      this.form.bookName = book.name
      this.form.score = book.score
      this.form.numbers = book.numbers

    },
    selectUser() {
      const user = this.users.find(v => v.username === this.form.userNo)
      this.form.userName = user.name
      this.form.userPhone = user.phone
      this.form.account = user.account
    }
  }
}
</script>